/*

filipesilvestrim - An open-source ActionScript 3.0 repository of codes for Game Developers 

http://code.google.com/p/filipesilvestrim/

Copyright (c) 2009 Filipe Ghesla Silvestrim, All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/


package com.filipesilvestrim.game.render.light
{
	import flash.display.BitmapData;
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	
	/**
	 * This Class represents an light spot in the view render
	 * 
	 * version 0.1
	 */
	public class SpotLight 
	{
		// ___________________________________________________________________ CONSTANTS
		
		// ___________________________________________________________________ CLASS PROPERTIES
		public var name			: String;
		public var x 			: Number;
		public var y 			: Number;
		public var width 		: Number;
		public var height 		: Number;

		private var _intensity 	: Number;
		private var _focus	 	: Number;
		
		// ___________________________________________________________________ INSTANCE PROPERTIES
		public var buffer		: BitmapData;
		public var spriteDebug 	: Sprite;
		
		private var _color		: Array = [0x0000ff, 0x0000ff];
		private var _alpha		: Array = [1, 0];
		private var _ratio		: Array = [0, 255];
		
		// ___________________________________________________________________ GETTERS AND SETTERS
		public function set focus ( value : Number ) : void
		{
			_focus = value;
			_ratio = [0, 255 * _focus];
		}
		
		public function set intensity ( value : Number ) : void
		{
			_intensity = value;
			_alpha = [_intensity, 0];
		}
		
		// ___________________________________________________________________ CONSTRUCTOR
		public function SpotLight(name : String, x : Number, y : Number, width : Number, height : Number)
		{
			this.name 	= name;
			this.x 		= x;
			this.y 		= y;
			this.width 	= width;
			this.height = height;
		}
		
		// ___________________________________________________________________ PUBLIC METHODS
		public function build() : void
		{
			buffer = generateRadialAlphaBox(width, height, _color, _alpha, _ratio); 
		}
		
		public function destroy() : void
		{
			buffer = null;
		}
		
		public function debug ( where : Sprite ) : void
		{
			spriteDebug = new Sprite();
			spriteDebug.name = name;
			spriteDebug.x = x;
			spriteDebug.y = y;
			spriteDebug.graphics.lineStyle(2, 0xff0000, .7);
			spriteDebug.graphics.beginFill(0xff0000, 0);
			spriteDebug.graphics.drawCircle(0,0,10);
			where.addChild(spriteDebug);
		}
		
		// ___________________________________________________________________ PRIVATE METHODS
		private function generateRadialAlphaBox ( width : Number, height : Number, colors : Array, alphas : Array, ratio : Array) : BitmapData
		{
			var bd 		: BitmapData 	= new BitmapData(width, height, true, 0x00000000);
			var matrix	: Matrix 		= new Matrix();
			var bound	: Sprite 		= new Sprite();

			matrix.createGradientBox(width,height,0,0,0);
			bound.graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratio, matrix);
			bound.graphics.drawRect(0,0,width,height);
			bound.graphics.endFill();
			bd.draw(bound);
			
			return bd;
		}
		
		// ___________________________________________________________________ EVENTS
		
		
		
	}
}